package twentytwenty;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-12 10:14
 * @Modified By:
 */
public class WordPattern_290 {
    public static void main(String[] args) {

    }

    public static boolean wordPattern(String pattern,String s){
        // 将字符串从空格处拆分，拆分成字符串数组
        String [] arrFromSpilt = s.split(" ");
        // 特殊情况判断
        int patternLength = pattern.length();
        if (pattern.length() != arrFromSpilt.length){
            return false;
        }
        for (int i = 0; i < patternLength; i++) {
            // 逐个判断，判断在pattern中的某个字母和arrFromSpilt数组中的某个单词出现的位置是否相同
            char letter = pattern.charAt(i);
            String word = arrFromSpilt[i];
            // 比较首次出现的位置是否相同
            if (pattern.indexOf(letter) != indexOfArray(arrFromSpilt,word)){
                return false;
            }
        }
        return true;
    }

    public static int indexOfArray(String [] arr,String word){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(word)){
                return i;
            }
        }
        return 0;
    }
}
